<?php
namespace Components\Parameters\Sha;

class Facade {
  public function findByKeyUseCacheIsDisabled($key = null) {
//     var_dump(json_encode(
//     [
//       'label'=>'等候天数'
//     ]
//     ));exit;
    /*
    var_dump(json_encode(
      [
        'static1' => [
          'disabled' => true,
          'name' => [
            'zh_CN'=>'排单',
          ],
          'value' => ['amount'=>5000,'average'=>5]
        ],
        'static2' => [
          'disabled' => true,
          'name' => [
            'zh_CN'=>'排单',
          ],
          'value' => ['amount'=>6000,'average'=>6]
        ],
        'static3' => [
          'disabled' => true,
          'name' => [
            'zh_CN'=>'排单',
          ],
          'value' => ['amount'=>7000,'average'=>7]
        ]
    ]
    ));exit;
    */

    $parameters = \Phalcon\Di::getDefault() -> getShared('cacheBackend') -> get('parameters');
    if(!$parameters) {
      $parameters = [];
      $params = Model::find();
      foreach($params as $param) {
        if($param -> disabled) {
          $parameters[$param -> key]['key'] = $param -> key;
          $parameters[$param -> key]['default'] = $param -> default;
          $parameters[$param -> key]['type'] = $param -> type;
          switch($param -> type) {
            case 148:
							$parameters[$param -> key]['value'] = $param -> value;
              break;
            case 168:
              foreach($param -> value as $k => $v) {
                if($v['disabled']) {
                  $parameters[$param -> key]['value'][$k] = $v;
                }
              }
              break;
          }
        }
      }
      \Phalcon\Di::getDefault() -> getShared('cacheBackend') -> save('parameters', $parameters);
    }
    
    if(is_null($key)) { return $parameters; }
    if(isset($parameters[$key])) { return $parameters[$key]; }
    return null;
  }
}
